package lg.crypto.aes

import lg.crypto.CryptByte

class AESByte(byte : Short) extends CryptByte(byte) {

  def *(other : AESByte) = {
    var counter : Int = 1
    var result : Int = 0x0
    var multiplier : Int = this.byte.asInstanceOf[Int]
    for (i <- 1 to 8) {
      var temp : Int = (counter & other.byte)
      if (temp != 0) {
        result = multiplier ^ result
      }
      counter = counter << 1
      multiplier = (multiplier & 0xff) << 1
      multiplier = if (multiplier >= 0x100) (multiplier ^ (0x1b)) else multiplier
    }
    new AESByte((result & 0xff).asInstanceOf[Short])
  }
}

object AESByte {
  def apply(long : Long) : AESByte = new AESByte(long.asInstanceOf[Short])
  def apply(int : Int) : AESByte = new AESByte(int.asInstanceOf[Short])
  def apply(short : Short) : AESByte = new AESByte(short)
  def apply(char : Char) : AESByte = new AESByte(char.asInstanceOf[Short])

  implicit def long2AESByte(long : Long) = AESByte(long)
  implicit def int2AESByte(int : Int) = AESByte(int)
  implicit def short2AESByte(short : Short) = AESByte(short)
  implicit def char2AESByte(char : Char) = AESByte(char)
}